package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.clientCenter.bean.OpenUserDetailListBean;
import com.koron.css2.clientCenter.bean.OpenUserDetailQueryBean;
import com.koron.css2.clientCenter.bean.UserOpenBean;
import com.koron.css2.clientCenter.mapper.OpenUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class OpenUserDetailList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		try {
			OpenUserDetailQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), OpenUserDetailQueryBean.class);
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			OpenUserMapper mapper = factory.getMapper(OpenUserMapper.class);
			List<OpenUserDetailListBean> list = mapper.getUserOpenListInfoById(queryBean.getId());
			
			UserOpenBean userOpenMessage = mapper.getUserOpenInfoById(queryBean.getId());// 获取开户记录信息
			for (OpenUserDetailListBean openUserDetailListBean : list) {
				// 获取营业区域
				if (!StringUtils.isEmpty(openUserDetailListBean.getBusinessArea())) {
					BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
					openUserDetailListBean.setBusinessAreaName(
							businessAreaMapper.findBusinessAreaById(openUserDetailListBean.getBusinessArea(), userInfo.getCurWaterCode()));
				}
				openUserDetailListBean.setUserOpenMessage(userOpenMessage);
			}

			@SuppressWarnings("rawtypes")
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		}catch (Exception e) {
			// TODO: handle exception
			logger.error("开户详情列表查询失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
